package com.sg.service.base.calc.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.calc.req.BatchQueryTargetEvaluationCrossDataListByDataTypesReq;
import com.sg.dto.base.calc.req.BatchQueryTargetEvaluationCrossDataListByIdsReq;
import com.sg.dto.base.calc.req.QueryTargetEvaluationCrossDataDetailReq;
import com.sg.dto.base.calc.req.QueryTargetEvaluationCrossDataListReq;
import com.sg.entity.OmsTargetIntegrateTempDataScope;
import com.sg.service.base.calc.MOmsTargetIntegrateTempDataScopeService;
import com.wicket.okrcalc.common.dto.FieldInfo;
import com.wicket.okrcalc.common.exception.BizException;
import com.wicket.okrcalc.dal.mapper.mbg.OmsTargetIntegrateTempDataScopeMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsTargetIntegrateTempDataScopeServiceImpl
        extends ServiceImpl<OmsTargetIntegrateTempDataScopeMapper, OmsTargetIntegrateTempDataScope>
        implements MOmsTargetIntegrateTempDataScopeService {

    @Autowired
    OmsTargetIntegrateTempDataScopeMapper omsTargetIntegrateTempDataScopeMapper;

    /**
     * 4-2-03新增目标跨模板数据范围[10146]
     * gen by moon at 8/24/2024, 11:00:36 AM
     */
    @Trace(operationName = "4-2-03新增目标跨模板数据范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addTargetEvaluationCrossData(OmsTargetIntegrateTempDataScope omsTargetIntegrateTempDataScope) {
        omsTargetIntegrateTempDataScope.setSingleWorkCycleDataId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsTargetIntegrateTempDataScope.setIsArchive("FALSE");
        omsTargetIntegrateTempDataScope.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsTargetIntegrateTempDataScope.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsTargetIntegrateTempDataScope != null && omsTargetIntegrateTempDataScope.getSubjectLifeCycle() != null && omsTargetIntegrateTempDataScope.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsTargetIntegrateTempDataScope.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsTargetIntegrateTempDataScope != null && omsTargetIntegrateTempDataScope.getSubjectLifeCycle() != null && omsTargetIntegrateTempDataScope.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsTargetIntegrateTempDataScope.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsTargetIntegrateTempDataScope.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsTargetIntegrateTempDataScope.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsTargetIntegrateTempDataScope != null && omsTargetIntegrateTempDataScope.getSubjectLifeCycle() != null && omsTargetIntegrateTempDataScope.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsTargetIntegrateTempDataScope.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsTargetIntegrateTempDataScope != null && omsTargetIntegrateTempDataScope.getSubjectLifeCycle() != null && omsTargetIntegrateTempDataScope.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsTargetIntegrateTempDataScope.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        omsTargetIntegrateTempDataScope.setIsValid("FALSE");
        omsTargetIntegrateTempDataScope.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsTargetIntegrateTempDataScope)) {
            return omsTargetIntegrateTempDataScope.getSingleWorkCycleDataId();
        } else {
            return "-1";
        }

    }

    /**
     * 4-2-03批量增目标跨模板数据范围[10147]
     * gen by moon at 8/21/2024, 11:39:53 PM
     */
    @Trace(operationName = "4-2-03批量增目标跨模板数据范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddTargetEvaluationCrossData(List<OmsTargetIntegrateTempDataScope> omsTargetIntegrateTempDataScope) {

        if (omsTargetIntegrateTempDataScope == null || omsTargetIntegrateTempDataScope.size() == 0) {
            return false;
        }

        return super.saveBatch(omsTargetIntegrateTempDataScope.stream().map(item -> {
            item.setSingleWorkCycleDataId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setSubjectLifeCycle("CURRENTLY_VALID");
            item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setIsArchive("FALSE");
            if (item != null && item.getSubjectLifeCycle() != null && item.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
                item.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
            }
            if (item != null && item.getSubjectLifeCycle() != null && item.getSubjectLifeCycle().equals("ARCHIVING")) {
                item.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
            }
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            if (item != null && item.getSubjectLifeCycle() != null && item.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
                item.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
            if (item != null && item.getSubjectLifeCycle() != null && item.getSubjectLifeCycle().equals("ARCHIVING")) {
                item.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
            }
            item.setIsValid("FALSE");
            item.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 4-2-03查询目标跨模板数据范围列表[10148]
     * gen by moon at 8/21/2024, 11:39:56 PM
     */
    @Trace(operationName = "4-2-03查询目标跨模板数据范围列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetIntegrateTempDataScope> queryTargetEvaluationCrossDataList(QueryTargetEvaluationCrossDataListReq reqDto) {

        try {
            QueryWrapper<OmsTargetIntegrateTempDataScope> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetIntegrateTempDataScope.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetIntegrateTempDataScope.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetIntegrateTempDataScope.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTargetIntegrateTempDataScope>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 4-2-03批量查询目标跨模板数据范围列表By数据归属主体ID[10149]
     * gen by moon at 8/21/2024, 11:40:00 PM
     */
    @Trace(operationName = "4-2-03批量查询目标跨模板数据范围列表By数据归属主体ID")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetIntegrateTempDataScope> batchQueryTargetEvaluationCrossDataListByIds(BatchQueryTargetEvaluationCrossDataListByIdsReq reqDto) {

        try {
            QueryWrapper<OmsTargetIntegrateTempDataScope> queryWrapper = new QueryWrapper<>();

            if (reqDto.getDataBelongSubjectList() == null || reqDto.getDataBelongSubjectList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("data_belong_subject_id", reqDto.getDataBelongSubjectList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetIntegrateTempDataScope.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetIntegrateTempDataScope.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetIntegrateTempDataScope.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTargetIntegrateTempDataScope>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 4-2-03查询目标跨模板数据范围详情[10173]
     * gen by moon at 8/24/2024, 11:00:40 AM
     */
    @Trace(operationName = "4-2-03查询目标跨模板数据范围详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsTargetIntegrateTempDataScope queryTargetEvaluationCrossDataDetail(QueryTargetEvaluationCrossDataDetailReq reqDto) {

        try {

            QueryWrapper<OmsTargetIntegrateTempDataScope> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsTargetIntegrateTempDataScope> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsTargetIntegrateTempDataScope retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 4-2-03修改目标跨模板数据范围[10174]
     * gen by moon at 8/24/2024, 11:00:41 AM
     */
    @Trace(operationName = "4-2-03修改目标跨模板数据范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateTargetEvaluationCrossData(OmsTargetIntegrateTempDataScope omsTargetIntegrateTempDataScope) {
        omsTargetIntegrateTempDataScope.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsTargetIntegrateTempDataScope.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(omsTargetIntegrateTempDataScope);
    }

    /**
     * 4-2-03删除目标跨模板数据范围[10175]
     * gen by moon at 8/24/2024, 11:00:42 AM
     */
    @Trace(operationName = "4-2-03删除目标跨模板数据范围")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteTargetEvaluationCrossData(String singleWorkCycleDataId) {

        return super.removeById(singleWorkCycleDataId);
    }

    /**
     * 4-2-03批量查询目标跨模板数据范围列表By数据点类型[10179]
     * gen by moon at 8/25/2024, 1:21:16 PM
     */
    @Trace(operationName = "4-2-03批量查询目标跨模板数据范围列表By数据点类型")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTargetIntegrateTempDataScope> batchQueryTargetEvaluationCrossDataListByDataTypes(BatchQueryTargetEvaluationCrossDataListByDataTypesReq reqDto) {

        try {
            QueryWrapper<OmsTargetIntegrateTempDataScope> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getDataTypesList() == null || reqDto.getDataTypesList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("data_type", reqDto.getDataTypesList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTargetIntegrateTempDataScope.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTargetIntegrateTempDataScope.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTargetIntegrateTempDataScope.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTargetIntegrateTempDataScope>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
